
package org.apache.solr.search;

import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;

/***
 * SortSpec encapsulates a Lucene Sort and a count of the number of documents
 * to return.
 */
public class SortSpec {

    Sort sort;
    int num;
    int offset;

    public SortSpec(Sort sort, int num) {
        this(sort, 0, num);
    }

    public SortSpec(Sort sort, int offset, int num) {
        this.sort = sort;
        this.offset = offset;
        this.num = num;
    }

    public void setSort(Sort s) {
        sort = s;
    }

    public static boolean includesScore(Sort sort) {
        if (sort == null) {
            return true;
        }
        for (SortField sf : sort.getSort()) {
            if (sf.getType() == SortField.Type.SCORE) {
                return true;
            }
        }
        return false;
    }

    public boolean includesScore() {
        return includesScore(sort);
    }

    /**
     * Gets the Lucene Sort object, or null for the default sort by score
     * descending.
     */
    public Sort getSort() {
        return sort;
    }

    /**
     * Offset into the list of results.
     */
    public int getOffset() {
        return offset;
    }

    /**
     * Gets the number of documents to return after sorting.
     *
     * @return number of docs to return, or -1 for no cut off (just sort)
     */
    public int getCount() {
        return num;
    }

    @Override
    public String toString() {
        return "start=" + offset + "&rows=" + num + (sort == null ? "" : "&sort=" + sort);
    }
}
